package net.oschina.dajiangnan.wx.process.msg;

import net.oschina.dajiangnan.wx.constants.Custom;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.ReceiveEvent;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.SendNews;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.SendText;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.XmlBean;
import net.oschina.dajiangnan.wx.process.msg.xmlbean.newsItems.Items;


public class EventClick {

    public XmlBean process(ReceiveEvent receiveEvent){
        
        String openid = receiveEvent.getFromUserName();
        String eventKey = receiveEvent.getEventKey();
        
        XmlBean respXmlBean = null;
        
        if(respXmlBean == null){
            String respMessage = Custom.wx.sendTextFromClickMenu(openid, eventKey);
            
            if(respMessage != null){
                respXmlBean = new SendText();
                ((SendText)respXmlBean).setMsgType("text");
                ((SendText)respXmlBean).setCreateTime(receiveEvent.getCreateTime());
                ((SendText)respXmlBean).setFromUserName(receiveEvent.getToUserName());
                ((SendText)respXmlBean).setToUserName(receiveEvent.getFromUserName());
                ((SendText)respXmlBean).setContent(respMessage);
            }
        }
        
        if(respXmlBean == null){
            Items items = Custom.wx.sendNewsFromClickMenu(openid, eventKey);
            if(items != null){
                respXmlBean = new SendNews();
                ((SendNews)respXmlBean).setCreateTime(receiveEvent.getCreateTime());
                ((SendNews)respXmlBean).setFromUserName(receiveEvent.getToUserName());
                ((SendNews)respXmlBean).setToUserName(receiveEvent.getFromUserName());
                ((SendNews)respXmlBean).setMsgType("news");
                ((SendNews)respXmlBean).setArticleCount(items.getItem().size());
                ((SendNews)respXmlBean).setItems(items);
            }
        }
        
        
        return respXmlBean;
    }
}
